package com.xw.controller.xw;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.controller.base.BaseController;
import com.xw.entity.Page;
import com.xw.entity.xw.Feedback;
import com.xw.entity.xw.Patient;
import com.xw.service.xw.feedback.FeedbackService;
import com.xw.util.AppUtil;
import com.xw.util.Jurisdiction;
import com.xw.util.PageData;

/**
 * 疗效反馈Controller
 * @author jeff
 *
 */

@Controller
@RequestMapping(value = "/feedback")
public class FeedbackController extends BaseController{


	@Resource(name = "feedbackService")
	private FeedbackService feedbackService;
	
	
	/**
	 * 获取疗效反馈列表
	 * @throws Exception 
	 * 
	 * 
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(Page page) throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData(); 
		pd = this.getPageData();
		String gjz = pd.getString("gjz");
		String beginTime = pd.getString("beginTime");
		if (beginTime != null && !"".equals(beginTime)) {
			beginTime = beginTime + " 00:00:00";
			pd.put("beginTime", beginTime);
		}
		String endTime = pd.getString("endTime");
		if (endTime != null && !"".equals(endTime)) {
			endTime = endTime + " 00:00:00";
			pd.put("endTime", endTime);
		}
		pd.put("gjz", gjz);
		page.setPd(pd);
				
		List<PageData> listAll  =  feedbackService.getList(page);
		mv.addObject("listAll", listAll);
		mv.setViewName("xw/feedback/feedback_list");
		return mv;
	}
	


	
	/**
	 * 添加疗效反馈界面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() throws Exception{
		ModelAndView mv = new ModelAndView();
		
		mv.setViewName("xw/feedback/addfeedback");
		return mv;
	}

	
	/**
	 * 新增疗效反馈
	 * @param session
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	public ModelAndView add() throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		Feedback feedBack=new Feedback();
		
		String hzName=pd.getString("hzxm");
		//hzName = new String(hzName.getBytes("ISO-8859-1"), "UTF-8");
		feedBack.setHzName(hzName);
		String fkPhone = pd.getString("fkdh");
		feedBack.setFkPhone(fkPhone);
		System.out.println("hzName:"+hzName);
		pd.put("hzName", hzName);
		pd.put("fkPhone", fkPhone);
		Patient patient = new Patient();
		patient =feedbackService.findHz(pd);//根据患者姓名电话查找患者ID
		long hzId = patient.getId();
		feedBack.setHzId(hzId);
		String fkContent=pd.getString("fknr");		
		feedBack.setFkContent(fkContent);
		String hfName = pd.getString("hfrxm");
		feedBack.setHfName(hfName);
		String hfTime = pd.getString("hfsj");
		feedBack.setHfTime(Timestamp.valueOf(hfTime));	
		System.out.println("hfTime1111"+hfTime);
		feedbackService.add(feedBack);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/**
	 * 删除疗效反馈
	 * @param response
	 * @param session
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public void delete(HttpServletRequest request) throws Exception{
		PageData pd = this.getPageData();
		String id = request.getParameter("id");
		
		pd.put("id",id);
		feedbackService.delete(pd);
	}
	
	
	
	/**
	 * 修改疗效反馈信息
	 * @param session
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd =  this.getPageData();
		
		String id = request.getParameter("id");
		id = URLDecoder.decode(id, "UTF-8");
		pd.put("id", id);
		System.out.println("ididididid:"+id);
				
		Feedback feedBack=feedbackService.getOne(pd);
		//String x = "Mon Mar 02 13:57:49 CST 2015";
	   // SimpleDateFormat sdf1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
	   // Date date=sdf1.parse(x);
	    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String hfTime=sdf.format(feedBack.getHfTime());
		System.out.println("反馈内容："+feedBack.getFkContent());
		System.out.println("反馈时间："+hfTime);
		mv.addObject("feedBack",feedBack);
		mv.addObject("hfTime",hfTime);
		mv.setViewName("xw/feedback/editfeedback");
		return mv;
	}
	
	
	
	/**
	 * 保存疗效反馈信息
	 * @throws Exception
	 */
	
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception {
		ModelAndView mv = this.getModelAndView();
		
		PageData pd = this.getPageData();
		Feedback feedBack=new Feedback();
		String id = pd.getString("id");
		System.out.println("编辑编辑idid:"+id);
		feedBack.setId(Long.valueOf(id));
		String hzName=pd.getString("hzxm");		
		feedBack.setHzName(hzName);
		String fkPhone = pd.getString("fkdh");	
		feedBack.setFkPhone(fkPhone);
		String fkContent=pd.getString("fknr");		
		feedBack.setFkContent(fkContent);
		String hfName = pd.getString("hfrxm");
		feedBack.setHfName(hfName);
		String hfTime = pd.getString("hfsj");
		feedBack.setHfTime(Timestamp.valueOf(hfTime));	
		System.out.println("hfTime1111"+hfTime);		
		feedbackService.edit(feedBack);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
		
	}

		
	/**
	 * 判断患者姓名和电话是否存在
	 * @throws Exception 
	 */
	@RequestMapping(value = "/hasU")
	@ResponseBody
	public Object hasU(HttpServletRequest request) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String errInfo = "success";
		PageData pd = new PageData();
		String hzxm = request.getParameter("hzxm");
		String hzdh = request.getParameter("hzdh");
		System.out.println("患者姓名是否存在："+hzxm);
		System.out.println("反馈电话是否存在："+hzdh);
		pd.put("hzName",hzxm);
		pd.put("fkPhone",hzdh);
		if (feedbackService.findHz(pd) == null) {
			errInfo = "error";
		}
		map.put("result", errInfo); // 返回结果
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	
	/**
	 * 批量删除
	 * @throws Exception 
	 */
	@RequestMapping(value = "/deleteAll")
	@ResponseBody
	public Object deleteAll() throws Exception {
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();

			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String USER_IDS = pd.getString("USER_IDS");
			System.out.println("USER_IDS:"+USER_IDS);

			if (null != USER_IDS && !"".equals(USER_IDS)) {
				String ArrayUSER_IDS[] = USER_IDS.split(",");
				feedbackService.deleteAll(ArrayUSER_IDS);
				pd.put("msg", "ok");
			} else {
				pd.put("msg", "no");
			}

			pdList.add(pd);
			map.put("list", pdList);
		return AppUtil.returnObject(pd, map);
	}
	
}
